﻿using EnterpriseWebsiteApi.ActionFilters;
using EnterpriseWebsiteApi.ActionResults;
using EnterpriseWebsiteApi.Dto.ProductCategorie;
using EnterpriseWebsiteBLL;
using EnterpriseWebsiteEntity;
using EnterpriseWebsiteUtil.Helper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EnterpriseWebsiteApi.Controllers.Admin
{
    [Route("api/admin/[controller]/[action]")]
    [ApiController]
    [TypeFilter(typeof(JwtAuthorizeFilter))]
    public class ProductCategorieController : ControllerBase
    {
        private readonly ProductCategorieService  _productCategorieService;
        private readonly Mapper _mapper;

        public ProductCategorieController(ProductCategorieService  productCategorieService, Mapper mapper)
        {
            _productCategorieService = productCategorieService;
            _mapper = mapper;
        }

        /// <summary>
        /// 插入或更新
        /// </summary>
        /// <param name="products"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> InsertOrUpdate(IEnumerable<ProductCategorieDTO> products)
        {
            try
            {
                var entitys =  _mapper.MapList<ProductCategorieEntity>(products);
                await _productCategorieService.InsertOrUpdate(entitys);
                return new Success();
            }
            catch (Exception ex)
            {
                return new Error(ex);
            }
        }

        /// <summary>
        /// 获取全部产品分类
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetAllProductCategorie()
        {
            try
            {
                var entitys = await _productCategorieService.GetAllProductCategorie();
                var dto = _mapper.MapList<ProductCategorieDTO>(entitys);
                return new Success(dto);
            }
            catch (Exception ex)
            {
                return new Error(ex);
            }
        }

        /// <summary>
        /// 根据ID删除一个产品分类
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> DeleteProductCategorieById(int id)
        {
            try
            {
                await _productCategorieService.DeleteProductCategorieById(id);
                return new Success();
            }
            catch (Exception ex)
            {
                return new Error(ex);
            }
        }
     }
}
